// =================================================================================================
//
//	CopyEngine Framework
//	Copyright 2012 Eran. All Rights Reserved.
//
//	This program is free software. You can redistribute and/or modify it
//	in accordance with the terms of the accompanying license agreement.
//
// =================================================================================================

/**
 * Created by eran on 14-12-21.
 */
package gui.component.mapEditor
{
    import data.DataVoManger;
    import data.MapVo;

    import flash.display.MovieClip;
    import flash.display.Sprite;
    import flash.events.MouseEvent;
    import flash.geom.Point;
    import flash.geom.Rectangle;
    import flash.text.TextField;

    import gui.component.mapEditor.block.MapBlockLayer;
    import gui.component.mapEditor.cursor.MapEditorCursor;
    import gui.component.mapEditor.grid.GridLayer;
    import gui.manger.GUIManger;
    import gui.utils.GUIUtils;

    public class MapEditorLayer
    {
        private var mEditLayerMc:MovieClip;
        private var mManger:GUIManger;

        //场景的根节点,格子层,鼠标层,Tile层全部在这之上
        private var mEditSceneRootContainer:Sprite;

        private var mGridLayer:GridLayer;
        private var mBlockLayer:MapBlockLayer;
        private var mCursor:MapEditorCursor;

        private var mTileAxisTF:TextField;

        private var mIsMouseDown:Boolean;
        private var mMouseDownPoint:Point;
        private var mLastMousePoint:Point;

        public function MapEditorLayer()
        {
        }

        //=================================//
        //=== Initialize & Dispose ========//
        //=================================//

        public function initialize(_editLayerMc:MovieClip, _manger:GUIManger, _xTileNum:int = 50,
                                   _yTileNum:int = 50):void
        {
            mEditLayerMc = _editLayerMc;
            mManger = _manger;

            mTileAxisTF = mEditLayerMc["infoMc"]["tileAxisTF"];

            mEditSceneRootContainer = new Sprite();
            mEditSceneRootContainer.graphics.beginFill(0, 0);
            mEditSceneRootContainer.graphics.drawRect(0,
                                                      0,
                                                      MapEditorConfig.EACH_BLOCK_SIZE * _xTileNum,
                                                      MapEditorConfig.EACH_BLOCK_SIZE * _yTileNum);
            mEditSceneRootContainer.graphics.endFill();
            mEditSceneRootContainer.mouseChildren = mEditSceneRootContainer.mouseEnabled = false;

            mEditLayerMc.scrollRect = new Rectangle(0, 0, mEditLayerMc.width, mEditLayerMc.height);
            GUIUtils.replaceTargetWithPH(mEditSceneRootContainer, mEditLayerMc["viewPortPH"]);

            mGridLayer = new GridLayer();
            mGridLayer.initialize(_xTileNum, _yTileNum);
            mEditSceneRootContainer.addChild(mGridLayer.container);

            mBlockLayer = new MapBlockLayer();
            mBlockLayer.initialize();
            mEditSceneRootContainer.addChild(mBlockLayer.container);

            mCursor = new MapEditorCursor();
            mCursor.initialize();
            mEditSceneRootContainer.addChild(mCursor.container);
            mCursor.moveCursorTo(0, 0);

            mMouseDownPoint = new Point();
            mLastMousePoint = new Point();

            addListener();
        }

        //=================================//
        //====== Public Function ==========//
        //=================================//

        public function refreshMapScene():void
        {
            var mapVo:MapVo = DataVoManger.instance.mapVo;

            mEditSceneRootContainer.graphics.beginFill(0, 0);
            mEditSceneRootContainer.graphics.drawRect(0,
                                                      0,
                                                      MapEditorConfig.EACH_BLOCK_SIZE * mapVo.xTileNum,
                                                      MapEditorConfig.EACH_BLOCK_SIZE * mapVo.yTileNum);
            mEditSceneRootContainer.graphics.endFill();

            mGridLayer.refresh(mapVo.xTileNum, mapVo.yTileNum);
        }

        public function refreshAllBlcokVo():void
        {
            mBlockLayer.refresh();
        }

        public function refreshAll():void
        {
            mBlockLayer.refresh();
            refreshMapScene();
        }

        //=================================//
        //====== Private Function =========//
        //=================================//

        private function addListener():void
        {
            mEditLayerMc.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMoveInViewPort);
            mEditLayerMc.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDownInViewPort);
            mEditLayerMc.addEventListener(MouseEvent.MOUSE_UP, onMouseUpInViewPort);
        }

        private function onMouseUpInViewPort(_e:MouseEvent):void
        {
            mIsMouseDown = false;
            if (Math.abs(mMouseDownPoint.x - _e.localX) <= 10 && Math.abs(mMouseDownPoint.y - _e.localY) <= 10)
            {
                //Click
                var xTileNum:int = Math.floor((_e.localX - mEditSceneRootContainer.x) /
                                              MapEditorConfig.EACH_BLOCK_SIZE);
                var yTileNum:int = Math.floor((_e.localY - mEditSceneRootContainer.y) /
                                              MapEditorConfig.EACH_BLOCK_SIZE);
                mManger.onClickTileInMap(xTileNum, yTileNum);
            }
        }

        private function onMouseDownInViewPort(_e:MouseEvent):void
        {
            mIsMouseDown = true;
            mLastMousePoint.x = mMouseDownPoint.x = _e.localX;
            mLastMousePoint.y = mMouseDownPoint.y = _e.localY;
        }


        private function onMouseMoveInViewPort(_e:MouseEvent):void
        {
            if (mIsMouseDown)
            {
                mEditSceneRootContainer.x += (_e.localX - mLastMousePoint.x);
                mEditSceneRootContainer.y += (_e.localY - mLastMousePoint.y);
            }
            else
            {
                var mapVo:MapVo = DataVoManger.instance.mapVo;

                var xTileNum:int = Math.floor((_e.localX - mEditSceneRootContainer.x) /
                                              MapEditorConfig.EACH_BLOCK_SIZE);
                var yTileNum:int = Math.floor((_e.localY - mEditSceneRootContainer.y) /
                                              MapEditorConfig.EACH_BLOCK_SIZE);

                mCursor.moveCursorTo(xTileNum, yTileNum);

                mTileAxisTF.text = "( " + xTileNum + " , " + yTileNum + " )";
            }
            mLastMousePoint.x = _e.localX;
            mLastMousePoint.y = _e.localY;
        }


    }
}
